package osSRC.schedule;


import java.util.LinkedList;

public class DynamicPriorityScheduler
		extends ProcessScheduler{
	DynamicPriorityScheduler(){}
	DynamicPriorityScheduler(LinkedList<PCB> queue){
		super(queue);
		this.currentTime=0;
		this.finishedQueue=new LinkedList<>();
		this.timeSlice=1;
		this.Titles[0]=super.Titles[0]+"\tPriority";
	}
	PCB getExecute(){
		PCB res=readyQueue.getFirst();
		for(PCB pcb:readyQueue){
			if(pcb.priority>res.priority)
				res=pcb;
		}
		return res;
	}
	@Override
	void scheduling() {
		PCB current;
		while (!readyQueue.isEmpty()){
			current=getExecute();
			System.out.println("Current Time:\t"+currentTime++);
			for (String s : Titles)			System.out.println(s);
			for (PCB pcb : readyQueue) {
				if (pcb == current)
					pcb.call(1);
				else
					pcb.wait(1);
			}

			for (PCB pcb : readyQueue)
				if(pcb.state.equals("W"))	System.out.println(pcb);
			if(!finishedQueue.isEmpty())	this.showFinishedQueue();
			if(current.checkFinished()){
				readyQueue.remove(current);
				finishedQueue.add(current);
			}

			System.out.print("Ready:\t\t");
			for (PCB pcb:readyQueue) 		System.out.print(pcb.processName+"\t");
			System.out.println();
			System.out.print("Finished:\t");
			for (PCB pcb:finishedQueue) 	System.out.print(pcb.processName+"\t");
			System.out.println();
		}
		for (String s:Titles)				System.out.println(s);
		for (PCB pcb:finishedQueue)			System.out.println(pcb);
	}
}
